package com.lzxuni.modules.productdisplay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.modules.common.entity.FileEntity;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.common.entity.Tree;
import com.lzxuni.modules.common.mapper.FileEntityMapper;
import com.lzxuni.modules.dataAcquisition.entity.Chain;
import com.lzxuni.modules.dataAcquisition.service.ChainService;
import com.lzxuni.modules.enterprise.entity.EnterpriseInfo;
import com.lzxuni.modules.enterprise.service.EnterpriseInfoService;
import com.lzxuni.modules.informationpublish.entity.InformationPublishPressRelease;
import com.lzxuni.modules.productdisplay.entity.ProductDisplay;
import com.lzxuni.modules.productdisplay.mapper.ProductDisplayMapper;
import com.lzxuni.modules.productdisplay.service.ProductDisplayService;
import com.lzxuni.modules.system.entity.BaseArea;
import com.lzxuni.modules.system.mapper.BaseAreaMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author llj
 * @since 2019-06-27
 */
@Service
public class ProductDisplayServiceImpl extends ServiceImpl<ProductDisplayMapper, ProductDisplay> implements ProductDisplayService {
    @Autowired
    private ProductDisplayMapper ProductDisplayMapper;
    @Autowired
    private ChainService chainService;
    @Autowired
    private FileEntityMapper fileEntityMapper;
    @Autowired
    private EnterpriseInfoService enterpriseInfoService;
    @Autowired
    private BaseAreaMapper baseAreaMapper;

    @Override
    public PageInfo<ProductDisplay> queryPage(PageParameter pageParameter, ProductDisplay productDisplay) throws SQLException {
        PageHelper.startPage(pageParameter.getPage(), pageParameter.getRows()).setOrderBy(
                pageParameter.getSidx() + " " + pageParameter.getSord());
        List<ProductDisplay> list;
        if (StringUtils.isNotEmpty(productDisplay.getProductName())) {
            list = baseMapper.selectList(new QueryWrapper<ProductDisplay>().like("product_name", productDisplay.getProductName()).orderByDesc("create_time"));
        }else {
            list = baseMapper.selectList(new QueryWrapper<ProductDisplay>().orderByDesc("create_time"));
        }
        if (list != null && list.size() > 0) {
            for (ProductDisplay productDisplay1 : list) {
                Chain chain = chainService.getById(productDisplay1.getTraceabilityChain());
                productDisplay1.setChainName(chain == null ? null : chain.getChainName());
            }
            for (ProductDisplay productDisplay2 : list) {
                EnterpriseInfo enterpriseInfo = enterpriseInfoService.getById(productDisplay2.getAffiliatedEnterprises());
                productDisplay2.setEnterpriseName(enterpriseInfo == null ? null : enterpriseInfo.getEnterpriseName());
            }
        }
        PageInfo<ProductDisplay> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
    @Override
    public List<Tree> getTreeSelectTree(String nature) {
        List<Tree> list = new ArrayList<>();
        List<ProductDisplay> productDisplays = baseMapper.selectList(new QueryWrapper<>());
        if (productDisplays != null && productDisplays.size() > 0) {
            for (ProductDisplay productDisplay : productDisplays) {
                Tree tree = new Tree();
                tree.setId(productDisplay.getProductName());
                tree.setParentId("");
                tree.setText(productDisplay.getProductName());
                tree.setTitle(productDisplay.getProductName());
                tree.setShowcheck(false);
                tree.setCheckstate(0);
                tree.setComplete(true);
                list.add(tree);
            }
        }
        return list;
    }
    @Override
    public List queryALL() throws Exception {
        List list = ProductDisplayMapper.queryALL();
        return list;
    }
    @Override
    public PageInfo<ProductDisplay> queryPagebm(PageParameter pageParameter) {
        PageHelper.startPage(pageParameter.getPage(), pageParameter.getRows()).setOrderBy(
                pageParameter.getSidx() + " " + pageParameter.getSord());
        List<ProductDisplay> list = baseMapper.selectList(new QueryWrapper<ProductDisplay>());
        List<FileEntity> list1 = null;
        List<BaseArea> list2 = null;
        List<BaseArea> list3 = null;
        List<BaseArea> list4 = null;
        List<BaseArea> list5 = null;
        List<BaseArea> list6 = null;
        String a = null;
        for (int i = 0;i < list.size();i++){
            FileEntity fileEntity = new FileEntity();
            fileEntity.setYwId(list.get(i).getYwId());
            fileEntity.setYwType("demopic1");
            list1 = fileEntityMapper.selectList(new QueryWrapper(fileEntity));
            if(list1 !=null&&list1.size()!=0){
                a= list1.get(0).getUrlPath();
                System.out.println(a);
                list.get(i).setProductpictures(a);
            }
            BaseArea baseArea = new BaseArea();
            if (list.get(i).getProvinceId() !=null&&list.get(i).getProvinceId().length()!=0){
                baseArea.setAreaId(list.get(i).getProvinceId());
                list2 =baseAreaMapper.selectList(new QueryWrapper(baseArea));
                list.get(i).setProvince(list2.get(0).getAreaName());
            }
            if (list.get(i).getCityId() !=null&&list.get(i).getCityId().length()!=0){
                baseArea.setAreaId(list.get(i).getCityId());
                list3 =baseAreaMapper.selectList(new QueryWrapper(baseArea));
                list.get(i).setCity(list3.get(0).getAreaName());
            }
            if (list.get(i).getCountyId() !=null&&list.get(i).getCountyId().length()!=0){
                baseArea.setAreaId(list.get(i).getCountyId());
                list4 =baseAreaMapper.selectList(new QueryWrapper(baseArea));
                list.get(i).setCounty(list4.get(0).getAreaName());
            }
            if (list.get(i).getTownId() !=null&&list.get(i).getTownId().length()!=0){
                baseArea.setAreaId(list.get(i).getTownId());
                list5 =baseAreaMapper.selectList(new QueryWrapper(baseArea));
                list.get(i).setTown(list5.get(0).getAreaName());
            }
            if (list.get(i).getVillageId() !=null&&list.get(i).getVillageId().length()!=0){
                baseArea.setAreaId(list.get(i).getVillageId());
                list6 =baseAreaMapper.selectList(new QueryWrapper(baseArea));
                list.get(i).setVillage(list6.get(0).getAreaName());
            }

        }
        PageInfo<ProductDisplay> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
